#include "TaskQueue.h"
#include <iostream>

using std::cout;
using std::endl;

TaskQueue::TaskQueue(int queSize)
    :_mutex()
     ,_notFull(_mutex)
     ,_notEmpty(_mutex)
     ,_queSize(queSize)
{}

TaskQueue::~TaskQueue()
{}

bool TaskQueue::empty() const
{
    return _que.empty();
}

bool TaskQueue::full() const
{
    return _que.size() >= _queSize;
}

void TaskQueue::push(const int& elem)
{
    MutexLockGuard mlg(_mutex);
    while(full())
    {
        _notFull.wait();
    }

    _que.push(elem);
    cout << "push :" << elem << " size :" << _que.size() << endl;
    _notEmpty.signal();
}

void TaskQueue::pop()
{
    MutexLockGuard mlg(_mutex);
    while(empty())
    {
        _notEmpty.wait();
    }

    cout << "pop :" << _que.front() << " size :" << _que.size() - 1 << endl;
    _que.pop();
    _notFull.signal();
}
